﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraTreeList;
    using DevExpress.XtraTreeList.Columns;
    using DevExpress.XtraTreeList.Nodes;
    using APP.HELPER;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using HRM.CORE;
    using HRM.CORE.REPORT;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using Common;

    public class xucInsurance : Common.xucBase
    {
        private Bar bar1;
        private Bar bar2;
        private BarDockControl barDockControlBottom;
        private BarDockControl barDockControlLeft;
        private BarDockControl barDockControlRight;
        private BarDockControl barDockControlTop;
        private BarManager barManager1;
        private BarButtonItem bbiInsuranceAdd;
        private BarButtonItem bbiInsuranceDelete;
        private BarButtonItem bbiInsuranceUpdate;
        private BarButtonItem bbiPrint;
        private BarButtonItem bbiReload;
        private BarButtonItem bbiView;
        private BarEditItem bbiYear;
        private Insurance cls;
        private IContainer components = null;
        private DataTable dt;
        public DevExpress.Utils.ImageCollection imageCollection2;
        private DevExpress.Utils.ImageCollection imgTree;
        private Point m_Point;
        private PopupMenu ppInsurance;
        private RepositoryItemTimeEdit repYear;
        private TreeList treeList1;
        private TreeListColumn treeListColumn2;

        public event SelectedEventHander Selected;

        public event UpdatedEventHander Updated;

        public xucInsurance()
        {
            this.InitializeComponent();
            clsAllOption option = new clsAllOption();
            this.bbiYear.EditValue = option.MonthDefault;
        }

        private void bbiInsuranceAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.InsuranceAdd();
        }

        private void bbiInsuranceDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                HRM_INSURANCE_TABLELIST hrm_insurance_tablelist = new HRM_INSURANCE_TABLELIST();
                object insuranceTableListID = this.cls.InsuranceTableListID;
                if (insuranceTableListID != null)
                {
                    base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                    if (hrm_insurance_tablelist.Get(new Guid(insuranceTableListID.ToString())) != "OK")
                    {
                        this.DoHide();
                        XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        this.DoHide();
                        string str = hrm_insurance_tablelist.Delete(insuranceTableListID.ToString());
                        if (str == "OK")
                        {
                            this.LoadData();
                        }
                        else if (str != "OK")
                        {
                            MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void bbiInsuranceUpdate_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.InsuranceUpdate();
        }

        private void bbiPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                new HRM.CORE.REPORT.xfmInsurance(this.cls.InsuranceTableListID).ShowDialog();
            }
            catch
            {
            }
        }

        private void bbiReload_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LoadData();
        }

        private void bbiView_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.LoadData();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void frm_Updated(object sender, HRM_INSURANCE_TABLELIST Item)
        {
            this.LoadData();
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xucInsurance));
            this.treeList1 = new TreeList();
            this.treeListColumn2 = new TreeListColumn();
            this.imgTree = new DevExpress.Utils.ImageCollection(this.components);
            this.barManager1 = new BarManager(this.components);
            this.bar1 = new Bar();
            this.bbiInsuranceAdd = new BarButtonItem();
            this.bbiInsuranceDelete = new BarButtonItem();
            this.bbiInsuranceUpdate = new BarButtonItem();
            this.bbiPrint = new BarButtonItem();
            this.bbiReload = new BarButtonItem();
            this.bar2 = new Bar();
            this.bbiYear = new BarEditItem();
            this.repYear = new RepositoryItemTimeEdit();
            this.bbiView = new BarButtonItem();
            this.barDockControlTop = new BarDockControl();
            this.barDockControlBottom = new BarDockControl();
            this.barDockControlLeft = new BarDockControl();
            this.barDockControlRight = new BarDockControl();
            this.imageCollection2 = new DevExpress.Utils.ImageCollection(this.components);
            this.ppInsurance = new PopupMenu(this.components);
            this.treeList1.BeginInit();
            this.imgTree.BeginInit();
            this.barManager1.BeginInit();
            this.repYear.BeginInit();
            this.imageCollection2.BeginInit();
            this.ppInsurance.BeginInit();
            base.SuspendLayout();
            this.treeList1.Appearance.FocusedCell.BackColor = Color.Orange;
            this.treeList1.Appearance.FocusedCell.BackColor2 = Color.White;
            this.treeList1.Appearance.FocusedCell.GradientMode = LinearGradientMode.BackwardDiagonal;
            this.treeList1.Appearance.FocusedCell.Options.UseBackColor = true;
            this.treeList1.Columns.AddRange(new TreeListColumn[] { this.treeListColumn2 });
            this.treeList1.Dock = DockStyle.Fill;
            this.treeList1.Location = new Point(0, 0x1a);
            this.treeList1.Name = "treeList1";
            this.treeList1.OptionsBehavior.PopulateServiceColumns = true;
            this.treeList1.OptionsView.ShowHorzLines = false;
            this.treeList1.OptionsView.ShowVertLines = false;
            this.treeList1.SelectImageList = this.imgTree;
            this.treeList1.Size = new Size(0x113, 0x1b7);
            this.treeList1.TabIndex = 3;
            this.treeList1.MouseDown += new MouseEventHandler(this.xucInsurance_MouseDown);
            this.treeList1.FocusedNodeChanged += new FocusedNodeChangedEventHandler(this.treeList1_FocusedNodeChanged);
            this.treeList1.GetSelectImage += new GetSelectImageEventHandler(this.treeList1_GetSelectImage);
            this.treeListColumn2.Caption = "Đợt b\x00e1o c\x00e1o";
            this.treeListColumn2.FieldName = "Name";
            this.treeListColumn2.Name = "treeListColumn2";
            this.treeListColumn2.OptionsColumn.AllowEdit = false;
            this.treeListColumn2.OptionsColumn.ReadOnly = true;
            this.treeListColumn2.Visible = true;
            this.treeListColumn2.VisibleIndex = 0;
      //      this.imgTree.ImageStream = (ImageCollectionStreamer) manager.GetObject("imgTree.ImageStream");
            this.imgTree.Images.SetKeyName(0, "database-icon.png");
            this.imgTree.Images.SetKeyName(1, "02a.png");
            this.imgTree.Images.SetKeyName(2, "03a.png");
            this.imgTree.Images.SetKeyName(3, "03b.png");
            this.barManager1.Bars.AddRange(new Bar[] { this.bar1, this.bar2 });
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Images = this.imageCollection2;
            this.barManager1.Items.AddRange(new BarItem[] { this.bbiReload, this.bbiInsuranceAdd, this.bbiInsuranceDelete, this.bbiInsuranceUpdate, this.bbiPrint, this.bbiYear, this.bbiView });
            this.barManager1.MaxItemId = 0x1d;
            this.barManager1.RepositoryItems.AddRange(new RepositoryItem[] { this.repYear });
            this.bar1.BarName = "Thanh c\x00f4ng cụ";
            this.bar1.DockCol = 0;
            this.bar1.DockRow = 0;
            this.bar1.DockStyle = BarDockStyle.Bottom;
            this.bar1.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(BarLinkUserDefines.PaintStyle, this.bbiInsuranceAdd, BarItemPaintStyle.Standard), new LinkPersistInfo(this.bbiInsuranceDelete, true), new LinkPersistInfo(this.bbiInsuranceUpdate, true), new LinkPersistInfo(this.bbiPrint, true), new LinkPersistInfo(this.bbiReload, true) });
            this.bar1.OptionsBar.AllowQuickCustomization = false;
            this.bar1.OptionsBar.DrawDragBorder = false;
            this.bar1.OptionsBar.UseWholeRow = true;
            this.bar1.Text = "Custom 2";
            this.bbiInsuranceAdd.Caption = "Th\x00eam đợt b\x00e1o c\x00e1o";
            this.bbiInsuranceAdd.Id = 0x17;
            this.bbiInsuranceAdd.ImageIndex = 0x45;
            this.bbiInsuranceAdd.Name = "bbiInsuranceAdd";
            this.bbiInsuranceAdd.ItemClick += new ItemClickEventHandler(this.bbiInsuranceAdd_ItemClick);
            this.bbiInsuranceDelete.Caption = "X\x00f3a đợt b\x00e1o c\x00e1o n\x00e0y";
            this.bbiInsuranceDelete.Id = 0x18;
            this.bbiInsuranceDelete.ImageIndex = 13;
            this.bbiInsuranceDelete.Name = "bbiInsuranceDelete";
            this.bbiInsuranceDelete.ItemClick += new ItemClickEventHandler(this.bbiInsuranceDelete_ItemClick);
            this.bbiInsuranceUpdate.Caption = "Sửa đợt b\x00e1o c\x00e1o n\x00e0y";
            this.bbiInsuranceUpdate.Id = 0x19;
            this.bbiInsuranceUpdate.ImageIndex = 14;
            this.bbiInsuranceUpdate.Name = "bbiInsuranceUpdate";
            this.bbiInsuranceUpdate.ItemClick += new ItemClickEventHandler(this.bbiInsuranceUpdate_ItemClick);
            this.bbiPrint.Caption = "In";
            this.bbiPrint.Id = 0x1a;
            this.bbiPrint.ImageIndex = 9;
            this.bbiPrint.Name = "bbiPrint";
            this.bbiPrint.ItemClick += new ItemClickEventHandler(this.bbiPrint_ItemClick);
            this.bbiReload.Caption = "Nạp lại";
            this.bbiReload.Id = 0x16;
            this.bbiReload.ImageIndex = 0x2a;
            this.bbiReload.Name = "bbiReload";
            this.bbiReload.ItemClick += new ItemClickEventHandler(this.bbiReload_ItemClick);
            this.bar2.BarName = "Custom 3";
            this.bar2.DockCol = 0;
            this.bar2.DockRow = 0;
            this.bar2.DockStyle = BarDockStyle.Top;
            this.bar2.FloatLocation = new Point(0x51, 0x250);
            this.bar2.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(BarLinkUserDefines.PaintStyle, this.bbiYear, BarItemPaintStyle.CaptionGlyph), new LinkPersistInfo(BarLinkUserDefines.PaintStyle, this.bbiView, "", true, true, true, 0, null, BarItemPaintStyle.CaptionGlyph) });
            this.bar2.OptionsBar.AllowQuickCustomization = false;
            this.bar2.OptionsBar.DrawDragBorder = false;
            this.bar2.OptionsBar.UseWholeRow = true;
            this.bar2.Text = "Custom 3";
            this.bbiYear.Caption = "Lọc theo năm:";
            this.bbiYear.Edit = this.repYear;
            this.bbiYear.EditValue = 0x7dc;
            this.bbiYear.Id = 0x1b;
            this.bbiYear.Name = "bbiYear";
            this.repYear.AutoHeight = false;
            this.repYear.Buttons.AddRange(new EditorButton[] { new EditorButton() });
            this.repYear.Mask.EditMask = "yyyy";
            this.repYear.Name = "repYear";
            this.bbiView.Caption = "Xem";
            this.bbiView.Id = 0x1c;
            this.bbiView.ImageIndex = 3;
            this.bbiView.Name = "bbiView";
            this.bbiView.ItemClick += new ItemClickEventHandler(this.bbiView_ItemClick);
//            this.imageCollection2.ImageStream = (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            this.imageCollection2.Images.SetKeyName(0x3d, "Vista (35).ico");
            this.imageCollection2.Images.SetKeyName(0x3e, "Security.ico");
            this.imageCollection2.Images.SetKeyName(0x3f, "RibbonPrintPreview_DocumentMapLarge.png");
            this.imageCollection2.Images.SetKeyName(0x40, "11.png");
            this.imageCollection2.Images.SetKeyName(0x41, "Sua.png");
            this.imageCollection2.Images.SetKeyName(0x42, "Them.png");
            this.imageCollection2.Images.SetKeyName(0x43, "PhanQuyen.png");
            this.imageCollection2.Images.SetKeyName(0x44, "11.png");
            this.imageCollection2.Images.SetKeyName(0x45, "plus.png");
            this.ppInsurance.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiInsuranceAdd), new LinkPersistInfo(this.bbiInsuranceUpdate, true), new LinkPersistInfo(this.bbiInsuranceDelete), new LinkPersistInfo(this.bbiReload, true) });
            this.ppInsurance.Manager = this.barManager1;
            this.ppInsurance.Name = "ppInsurance";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
//            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.treeList1);
            base.Controls.Add(this.barDockControlLeft);
            base.Controls.Add(this.barDockControlRight);
            base.Controls.Add(this.barDockControlBottom);
            base.Controls.Add(this.barDockControlTop);
            base.Name = "xucInsurance";
            base.Size = new Size(0x113, 0x1eb);
            this.treeList1.EndInit();
            this.imgTree.EndInit();
            this.barManager1.EndInit();
            this.repYear.EndInit();
            this.imageCollection2.EndInit();
            this.ppInsurance.EndInit();
            base.ResumeLayout(false);
        }

        public void InsuranceAdd()
        {
            xfmInsuranceAdd add = new xfmInsuranceAdd(Actions.Add);
            add.Added += new xfmInsuranceAdd.AddedEventHander(this.xfm_Added);
            add.ShowDialog();
        }

        public void InsuranceUpdate()
        {
            try
            {
                HRM_INSURANCE_TABLELIST item = new HRM_INSURANCE_TABLELIST();
                object insuranceTableListID = this.cls.InsuranceTableListID;
                if (insuranceTableListID != null)
                {
                    base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                    if (item.Get(new Guid(insuranceTableListID.ToString())) != "OK")
                    {
                        this.DoHide();
                        XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        this.DoHide();
                        xfmInsuranceAdd add = new xfmInsuranceAdd(Actions.Update, item);
                        add.Updated += new xfmInsuranceAdd.UpdatedEventHander(this.frm_Updated);
                        add.ShowDialog();
                    }
                }
            }
            catch
            {
            }
        }

        public void LoadData()
        {
            this.LoadTreeList(System.DateTime.Parse(this.bbiYear.EditValue.ToString()).Year);
        }

        private void LoadTreeList(int Year)
        {
            DataTable listByYear = new HRM_INSURANCE_TABLELIST().GetListByYear(Year);
            this.dt = new DataTable();
            this.dt.Columns.Add("ID");
            this.dt.Columns.Add("Name");
            this.dt.Columns.Add("ParentID");
            this.dt.Columns.Add("Type");
            this.dt.Columns.Add("ImageIndex");
            foreach (DataRow row in listByYear.Rows)
            {
                this.dt.Rows.Add(new object[] { row["InsuranceTableListID"].ToString(), row["InsuranceTableListName"].ToString(), "", 0, 0 });
                this.dt.Rows.Add(new object[] { row["InsuranceTableListID"].ToString() + "1", "Mẫu số 02a-TBH [1]", row["InsuranceTablelIstID"].ToString(), 1, 1 });
                this.dt.Rows.Add(new object[] { row["InsuranceTableListID"].ToString() + "2", "Mẫu số 03a-TBH [2]", row["InsuranceTableListID"].ToString(), 2, 2 });
                this.dt.Rows.Add(new object[] { row["InsuranceTableListID"].ToString() + "3", "Mẫu số 03b-TBH [3]", row["InsuranceTableListID"].ToString(), 3, 3 });
            }
            this.treeList1.DataSource = this.dt;
            try
            {
                this.treeList1.Nodes.FirstNode.ExpandAll();
                this.NodeSelected(this.treeList1.Nodes.FirstNode);
            }
            catch
            {
            }
        }

        private void NodeSelected(TreeListNode Node)
        {
            DataRowView dataRecordByNode = (DataRowView) this.treeList1.GetDataRecordByNode(Node);
            this.cls = new Insurance();
            if (Node != null)
            {
                if (Node.Level == 0)
                {
                    this.cls.InsuranceTableListID = new Guid(dataRecordByNode["ID"].ToString());
                }
                else if (Node.Level == 1)
                {
                    this.cls.InsuranceTableListID = new Guid(dataRecordByNode["ParentID"].ToString());
                }
                this.cls.Type = int.Parse(dataRecordByNode["Type"].ToString());
            }
            this.RaiseSelectedEventHander(this.cls);
            this.treeList1.SetFocusedNode(Node);
        }

        public void RaiseSelectedEventHander(Insurance Item)
        {
            if (this.Selected != null)
            {
                this.Selected(this, Item);
            }
        }

        public void RaiseUpdatedEventHander()
        {
            if (this.Updated != null)
            {
                this.Updated(this);
            }
        }

        private void ShowMenu(TreeListHitInfo hi, Point point)
        {
            this.treeList1.SetFocusedNode(hi.Node);
            if (hi.Node != null)
            {
                this.ppInsurance.ShowPopup(point);
            }
        }

        private void treeList1_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            this.NodeSelected(e.Node);
        }

        private void treeList1_GetSelectImage(object sender, GetSelectImageEventArgs e)
        {
            DataRowView dataRecordByNode = (DataRowView) this.treeList1.GetDataRecordByNode(e.Node);
            e.NodeImageIndex = int.Parse(dataRecordByNode["ImageIndex"].ToString());
        }

        private void xfm_Added(object sender, HRM_INSURANCE_TABLELIST Item)
        {
            this.LoadData();
        }

        private void xucInsurance_MouseDown(object sender, MouseEventArgs e)
        {
            this.m_Point = new Point(e.X, e.Y);
            if (e.Button == MouseButtons.Right)
            {
                this.ShowMenu(this.treeList1.CalcHitInfo(this.m_Point), this.treeList1.PointToScreen(this.m_Point));
            }
        }

        public delegate void SelectedEventHander(object sender, Insurance Item);

        public delegate void UpdatedEventHander(object sender);
    }
}

